package posecco.shared;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import posecco.client.User;

import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.gwtent.reflection.client.ClassType;
import com.gwtent.reflection.client.NotFoundException;
import com.gwtent.reflection.client.TypeOracle;

/**
 * <p>
 * This is some utility class to handle form fields
 * </p>
 */

public class FieldUtil {

	private Map<String, Object> fields = new HashMap<String, Object>();	
	
	public FieldUtil() {
		
		
	}
	
	public Map<String, Object> getFields() {
		return fields;
	}

	public void setFields(Map<String, Object> field) {
		this.fields = field;
	}

	public void addFormTextField(Panel myPanel, String name) {

		FieldRow fieldRow = new FieldRow(name, "Input");
		fields.put(name, fieldRow);
		buildFieldRow(myPanel, fieldRow);
	}

	public void addFormPwdField(Panel myPanel, String name) {

		FieldRow fieldRow = new FieldRow(name, "PwdInput");
		fields.put(name, fieldRow);
		buildFieldRow(myPanel, fieldRow);
	}
	
	public void addRoleSelectionField(Panel myPanel, String name) {

		FieldRow fieldRow = new FieldRow(name, "RoleSelect");
		fields.put(name, fieldRow);
		buildFieldRow(myPanel, fieldRow);
	}
	
	private void buildFieldRow(Panel myPanel, FieldRow fieldRow) {
		
		myPanel.add(fieldRow.getFieldLabel());
		myPanel.add(fieldRow.getField());
		myPanel.add(fieldRow.getErrorLabel());
	}
	
	public String getFieldValue(String fieldName) {
		
		try {
			
			FieldRow fieldRow = (FieldRow) fields.get(fieldName);
			return ((TextBox) fieldRow.getField()).getValue().toString();
		}
		catch (NullPointerException e) {
						
		}
		return "";
	}
	
	public void setErrorMessage(String fieldName, String errorMessage) {
		
		try {
			
			((FieldRow) fields.get(fieldName)).getErrorLabel().setText(errorMessage);
		}
		catch (NullPointerException e) {
			
		}
	}
	
	@SuppressWarnings("rawtypes")
	public void setFieldValues(Object values) {
		
		try {
			
			Iterator<Entry<String, Object>> it = fields.entrySet().iterator();
		    while (it.hasNext()) {
		        Map.Entry<String, Object> pairs = (Map.Entry<String, Object>)it.next();
		        if (pairs.getKey().indexOf("pwd") > -1) {
		        	
		        	((FieldRow) fields.get(pairs.getKey())).getField().setVisible(false);
		        	((FieldRow) fields.get(pairs.getKey())).getFieldLabel().setVisible(false);
		        	((FieldRow) fields.get(pairs.getKey())).getErrorLabel().setVisible(false);
		        }
		        else {
		        	
		        	try {
		        		
						ClassType classType = TypeOracle.Instance.getClassType(User.class);
						String getterName = "get" + pairs.getKey().substring(0, 1).toUpperCase() + pairs.getKey().substring(1);
				        ((FieldRow) fields.get(pairs.getKey())).setFieldValue(classType.invoke(values, getterName, (Object[])null).toString());
		        	}
		        	catch(NotFoundException e1) {}
		        }
		    }
		}
		catch (NullPointerException e) {
			
		}
	}
	
	public Widget getField(String fieldName) {
		
		return ((FieldRow) fields.get(fieldName)).getField();
	}
	
	public Label getFieldLabel(String fieldName) {
		
		return ((FieldRow) fields.get(fieldName)).getFieldLabel();
	}
	
	public Label getErrorLabel(String fieldName) {
		
		return ((FieldRow) fields.get(fieldName)).getErrorLabel();
	}
	
	public void removeErrorMessages() {
		
		Iterator<Entry<String, Object>> it = fields.entrySet().iterator();
	    while (it.hasNext()) {
	        Map.Entry<String, Object> pairs = (Map.Entry<String, Object>)it.next();
	        ((FieldRow) pairs.getValue()).getErrorLabel().setText("");
	    }
	}
}
